$(document).ready(function () {
  $('#qushangnmian').click(function () {
    $('html,body').animate({scrollTop: 0}, 'slow')
  })
})



// 第二页往下的数据
// 页面的Ajax请求
// var pageIndex = 0
// 请求所有商品的列表
var pageIndex = 1 // 当前页
var total = 0 // 总页数
var ul = document.querySelector('.myPagination>ul')
var arr = null
getData()
function getData () {
  $.ajax({
    url: '/lieBia',
    data: {
      copuonCode: '',
      keyword: '',
      tagType: 3,
      tagValue: '1088;1089;1090;1091;1092;1093;1094;1095;1096;1097;1099;1101;1098;1100',
      pageIndex: pageIndex
    },
    success: res => {
      // console.log(res)
      console.log(res.result)
      var data = res.result
      // console.log(res.result.pagination.pageCount)
      if (res.status === 200) {
        // 渲染品牌
        var a = $('#pinpai')
        data.Brands.forEach(item => {
          var span = $(`<a href="javascript:void(0)" class="value-item" title="${item.enbrand}"><span class="txt"  data-key="ibrandid" data-value=10169 keyword="护手霜" brandid="10169" module_index
          data-op="or">${item.enbrand}</span>
          <!-- <i class="home-icon hi-delete"></i> -->
          </a>`)
          a.append(span)
        })
        // 买手地区
        var mei = $('#mei')
        data.SellerCountrys.forEach(item => {
          var span = $(`<a href="javascript:void(0)" categoryid="1113" module_index keyword="" class="value-item"
          title="${item.countryName}"><span class="txt" data-key="ithirdcategoryid" data-value="1113"
              data-op="or">${item.countryName}</span><i class="home-icon hi-delete"></i></a>`)
          mei.append(span)
        })
        // 面部护肤
        var mian = $('#mian')
        data.ThirdCategorys[0].thirdId.forEach(item => {
          var span = $(`<a href="javascript:void(0)" categoryid="1093" module_index keyword="" class="value-item"
          title="${item.name}"><span class="txt" data-key="ithirdcategoryid" data-value="1093"
           data-op="or">${item.name}</span><i class="home-icon hi-delete"></i></a>`)
          mian.append(span)
        })
        // 列表页的数据

        // 分页//目的：数据
        // arr = res.result.Products
        // new Page('myPagination', {
        //   pageData: {
        //     total: 50,
        //     pageSize: 5 // 一页有多少个数据
        //   },
        //   show: function (currentPage) {
        //     $('.search-list>.product-list').html('')
            // var data = arr.slice((currentPage - 1) * 5, currentPage * 5)
            // console.log(data)
            var li = ''
            data.Products.forEach(item => {
            
              // console.log(item)
              li += `
                <li class="product-item" >
                <a href="detial.html?id=${item.ProductId}">
                    <img src="${item.MainPic}" alt="">
                </a>
                <p class="price">
                    <em class="unit">￥</em><span>${item.MinPrice}</span>
                    <span class="type">
                        <em>包邮包税</em>
                    </span>
                </p>
                <a href="detial.html?id=${item.ProductId}"><p class="name" title="${item.Title}">${item.Title}</p></a>
                <div class="seller-site">
                    <a href=" " class="seller">
                    <img style="height: 20px;width: 20px; border-radius: 50%;display: inline;"   src="${item.sellerLogo}" alt="">
                         <span class="txt">${item.SellerName}</span> 
                    </a>
                   <a href="" class="site">
                        <span> <img style="height: 20px;width: 20px; border-radius: 50%;display: inline;"   src="${item.countryFlag}" alt=""></span>
                       <span class="txt">${item.countryNameZh}</span>
                   </a>
                </div>
            </li>`
              // 把商品id放在地址栏带到细节的地址栏里
              // pageIndex = res.result.pagination.page

            })
            $('.search-list>.product-list').append(li)
            // var a =$('.product-list li')
          // console.log(a);
            // for (var i = 0; i < a.length; i++){
            //   a[i].onclick = function () {
            //     var goodsid = $(this).attr('goodid')
            //     window.localStorage.setItem('goodid', goodsid)
            //     var item = data.find(item => {
            //       return item.ProductId==goodsid
            //     })
            //     item = JSON.stringify(item)
            //     window.localStorage.setItem('item',item)
            //     // location.href = './detail.html' 
            //   }

            //  }
          // }
        // })
      }
    }
  })
}

//

// 点击更多后，高度成为140px
document.getElementById('more-text').addEventListener('click', function fn () {
  document.getElementById('pinpai').style.height = '140px'
  document.getElementById('more-text').style.display = 'none'
  document.getElementById('upupup').style.display = 'block'
})
document.getElementById('upupup').addEventListener('click', function fn () {
  document.getElementById('pinpai').style.height = '70px'
  document.getElementById('more-text').style.display = 'block'
  document.getElementById('upupup').style.display = 'none'
})

// 登录注册模块
// 如果在本地里没有名字
fn()
$('.dengdeng1').on('click', function () {
  localStorage.setItem('url', location.href)
  location.href = '../login.html'
})
function fn () {
  // 在本地里获取名字
  var name = window.localStorage.getItem('name')
  if (!name) {
    $('.page-top>.container>.top-fr>div').html(`
    <a href=" javascript:;"><span class="dengdeng1" >登录</span></a>
    <a href="./register.html"><span>注册</span></a>
    `)
  }else {
    // name存在
    $('.page-top>.container>.top-fr').css('position', 'relative')
    $('.page-top>.container>.top-fr>div').html(`<a href="" class="info"><span>${name}</span><span></span>
      <ul><li onclick="logout()" >退出登录</li>
          <li>修改密码</li>
          <li>我的订单</li></ul>
       </a>`).css({ 'position': 'absolute','left': '-82px','z-index': '5'})
  }
}
// 退出登录
function logout () {
  fn()
  // 移除本地的数据
  window.localStorage.removeItem('name')
  window.localStorage.removeItem('userInfo')
  window.localStorage.removeItem('data')
}
